package Demo05;
import java.util.Arrays;

class student implements Comparable<student>{
    public String name;
    public int age;
    public int score;

    public student(String name,int age,int score){
        this.name=name;
        this.age=age;
        this.score=score;
    }

    @Override
    public String toString(){
        return "student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                "}\n";
    }

    @Override
    public int compareTo(student o) {
        if (this.age > o.age) {
            return 1;
        } else if (this.age < o.age) {
            return -1;
        } else {
            return 0;
        }
    }
}
public class test {
    public static void sort(Comparable[] array){
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j].compareTo(array[j+1]) > 0) {
                    Comparable tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        student[] students=new student[3];
        students[0] =new student("张三",10,30);
        students[1] =new student("李四",20,40);
        students[2] =new student("王五",30,50);

        sort(students);

        System.out.println(Arrays.toString(students));
    }

//    public static void main(String[] args) {
//        int[] array = {1,3,2,10,5,3,7};
//        Arrays.sort(array);
//        System.out.println(Arrays.toString(array));
//    }

}





